<?php
include './jwt.php';
use Auth\JwtToken;
header('Content-Type: application/json;charset=UTF-8');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
$user = $_REQUEST['user'];
$pass = $_REQUEST['pass'];

// 假设这里有一个验证用户密码的逻辑
$userValidator = new UserValidator();
$isValidUser = $userValidator->checkUser($user, $pass); // 你需要实现这个方法

if ($isValidUser) {
    $jwtHandler = new JwtToken();
    $generatedJwt = $jwtHandler->generateJwt($user);
    echo json_encode(['code' => 200, 'status' => 'success', 'Authorization' => 'Bearer ' . $generatedJwt]);
} else {
    echo json_encode(['code' => 401, 'status' => 'error', 'message' => 'Invalid user or password']);
}

// 示例验证用户密码的方法
class UserValidator {
    private function validateUser($user, $pass) {
        if ($user == 'admin' && $pass == '123456') {
            return true;
        } else {
            return false;
        }
    }

    public function checkUser($user, $pass) {
        return $this->validateUser($user, $pass);
    }
}
?>